<?php

/**
 * uniBoard ACP Topic Icons
 *
 * Light and fast forum solution for web 2.0-like communities.
 *
 * @package		uniBoard
 * @author		Rafał Pitoń
 * @license		http://www.gnu.org/licenses/gpl-3.0.html
 * @since		Version 0.1
 * 
 * ------------------------------------------------------------------------
 * 
 * $Date: 2010-09-15 17:35:38 +0000 (Wed, 15 Sep 2010) $
 * $Revision: 61 $
 * $Author: rafio.xudb@gmail.com $
 */

// ------------------------------------------------------------------------

class action_icons{
	
	/**
	 * Main class pointer
	 *
	 * @var object
	 */
	
	private $appCore		= NULL;
	
	// ------------------------------------------------------------------------

	/**
	 * Initialise Action
	 *
	 * @param object $appCore
	 */
	
	function __construct( $appCore){
		
		// Store appCore pointer
		$this -> appCore = $appCore;
		
		// Add language string
		$appCore -> output -> js_vars['l_delete_selected_items_sure'] =  $appCore -> lang['delete_selected_items_sure'];
			
		// Load fields templates
		$appCore -> templates['form_inputs'] = new tpl_form_inputs( $appCore);
		
		// Delete Icon
		if ( $appCore -> _REQ( 'do') == 'delete')
		{
			// Get Icon api
			$icon_api = new api_icon( $appCore, $appCore -> _REQ( 'icon'));
			
			// Found it?
			if ( $icon_api -> data_loaded)
			{
				// Delete
				$icon_api -> deleteData();
				
				// Set Log
				$appCore -> logAdmin( 'log_admin_deleted_icon', htmlspecialchars( $icon_api -> icon_image));
				
				// Draw message
				$appCore -> output -> message( $appCore -> lang['acp_icon_deleted'], $appCore -> lang['acp_topic_icons_list']);
			}
			else
			{
				// Draw message
				$appCore -> output -> error( $appCore -> lang['acp_icon_not_found'], $appCore -> lang['acp_topic_icons_list']);
			}
		}
		
		// Delete many icons
		if ( $appCore -> _POST( 'do') == 'mass_delete')
		{
			// Selected icons
			$icons = $appCore -> listInput( 'select_icon');
			
			// Anything selected?
			if ( empty( $icons))
			{
				// Draw message
				$appCore -> output -> error( $appCore -> lang['nothing_selected'], $appCore -> lang['acp_topic_icons_list']);
			}
			else
			{
				// Delete data
				$appCore -> db -> delete( 'topic_icons', 'icon_id IN (' . join( ',', $icons) . ')');
				
				// Set Log
				$appCore -> logAdmin( 'log_admin_mass_deleted_icons');
				
				// Draw message
				$appCore -> output -> message( $appCore -> lang['acp_icon_deleted_m'], $appCore -> lang['acp_topic_icons_list']);
			}
		}
		
		// Cache output
		ob_start();
		
		// Open table block
		echo $appCore -> output -> openTableBlock();
				
		// Select Icons
		$appCore -> db -> query( 'SELECT *
		FROM ' . DB_PREFIX . 'topic_icons
		ORDER BY icon_image ASC');
		
		// Draw header
		echo $appCore -> output -> tableHeader( array(
			array(
				'left_float width_big',
				$appCore -> lang['icons_list_icon']
			),
			array(
				'right_float width_tiny center',
				( $appCore -> db -> numRows() > 0 ? $appCore -> drawCheckbox( '', false, 'checkbox_master') : '')
			),
			array(
				'right_float width_medium',
				$appCore -> lang['actions']
			)
		));
		
		// Open Mass-Delete forum?
		if ( $appCore -> db -> numRows() > 0)
		{
			// Open form
			echo $appCore -> output -> openForm( $appCore -> adminLink( 'act=icons'));
			echo $appCore -> drawHidden( 'do', 'mass_delete');
		}
		
		// Parse result
		while ( $icon_result = $appCore -> db -> fetch_array())
		{
			// Draw row
			echo $appCore -> output -> tableRow('
				<div class="cell left_float width_small center">
					<img src="' . TOPICS_ICONS_PATH . htmlspecialchars( $icon_result[1]) . '" width="' . $icon_result[2] . '" height="' . $icon_result[3] . '" alt="" title="" />
				</div>
				<div class="cell left_border right_float width_tiny center">
					' . $appCore -> drawCheckbox( 'select_icon[' . $icon_result[0] . ']', false, 'checkbox_children') . '
				</div>
				<div class="cell left_border right_float width_medium center">
					<a href="' . $appCore -> adminLink( 'act=icons&do=delete&icon=' . $icon_result[0]) . '">' . $appCore -> output -> drawImage( 'action_delete', $appCore -> lang['delete']) . '</a>
				</div>');
		}
		
		// Delete Selected Button?
		if ( $appCore -> db -> numRows() > 0)
		{
			// Draw button
			echo $appCore -> output -> submitButton( $appCore -> lang['delete_selected'], '', 'delete_items_confirm');
			
			// Close form
			echo $appCore -> output -> closeForm();
		}
		
		// Free result
		$appCore -> db -> freeResult();
		
		// Close table block
		echo $appCore -> output -> closeTableBlock();
		
		// New Icon Button
		echo $appCore -> output -> openForm( $appCore -> adminLink( 'act=new_icon'));
		echo $appCore -> output -> openFormBlock( $appCore -> lang['new_icon_form']);
		echo $appCore -> output -> submitButton( $appCore -> lang['new_icon_form_submit']);
		echo $appCore -> output -> closeFormBlock();
		echo $appCore -> output -> closeForm();
		
		// Draw output
		$appCore -> output -> draw( $appCore -> lang['acp_topic_icons_list'], array( $appCore -> adminLink( 'act=icons') => $appCore -> lang['acp_topic_icons_list']), ob_get_clean());
				
	}

}